package cn.orz.study.option.service.impl;

import cn.orz.study.dto.BasePageQuery;
import cn.orz.study.option.entity.MyUuidBigtabEntity;
import cn.orz.study.option.mapper.MyUuidBigtabMapper;
import cn.orz.study.option.service.IMyUuidBigtabService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.Slave;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tangmingze
 * @since 2021-11-19
 */
@Service
@Slf4j
@Slave
public class MyUuidBigtabServiceImpl extends ServiceImpl<MyUuidBigtabMapper, MyUuidBigtabEntity> implements IMyUuidBigtabService {

    @Override
    public Flux<MyUuidBigtabEntity> getAllOfOffset(LambdaQueryWrapper<MyUuidBigtabEntity> q) {
        return Flux.create(fs -> {
            try {
                getBaseMapper().listAsStream(q, resultContext -> {
                    if (!resultContext.isStopped() || resultContext.getResultObject() != null) {
                        val count = resultContext.getResultCount();
                        val res = resultContext.getResultObject();
                        fs.next(res);
                    } else {
                        log.info("停止");
                    }
                });
            } catch (Exception e) {
                log.error("err", e);
                fs.error(e);
            }
            fs.complete();
        });
    }

    @Override
    public IPage<MyUuidBigtabEntity> page(BasePageQuery query) {
        MyUuidBigtabEntity one = getBaseMapper().getSqlLimitOne((long)query.getOffset());
        Page<MyUuidBigtabEntity> page = query.createPage();
        page.setTotal(count());
//        return page(query.createPage());
        return page(page,
                Wrappers.lambdaQuery(MyUuidBigtabEntity.class)
                        .ge(MyUuidBigtabEntity::getId,one.getId())
        );
    }
}
